﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FreshCms.BackEnd.ControlsCommon
{
    public partial class Pager : System.Web.UI.UserControl
    {
        private int _index = 0;
        private string _cate = string.Empty;
        private string _extention;

        public int Index
        {
            get { return _index; }
            set { _index = value; }
        }

        public string Cate
        {
            get { return _cate.TrimStart('/'); }
            set { _cate = value; }
        }

        public string Extention
        {
            //get { return _extention != "" && _extention != null ? _extention : ".chn"; }
            get { return _extention; }
            set { _extention = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            {
                this.PreRender += new EventHandler(Pagging_PreRender);
            }
        }

        void Pagging_PreRender(object sender, EventArgs e)
        {
            try
            {
                StringBuilder b = new StringBuilder("");

                if (Index <= 1)
                {
                    Index = 1;
                }
                else
                {
                    if (Index == 2)
                    {
                        b.Append("<li class=\"prev\"><a href='/" + Cate + Extention + "' >«</a></li>");
                    }
                    else
                    {
                        b.Append("<li class=\"prev\"><a href='/" + Cate + "&page=" + Convert.ToString(Index - 1) + Extention + "' >«</a></li>");
                    }
                }


                if (Index <= 7)
                {
                    for (int i = 1; i < 10; i++)
                    {
                        if (i == Index)
                        {
                            b.Append("<li class=\"active\"><a href=\"javascript:void(0)\">" + i.ToString() + "</a></li>");
                        }
                        else
                        {
                            if (i == 1)
                            {
                                b.Append("<li><a href='/" + Cate + Extention + "' >" + i.ToString() + "</a></li>");
                            }
                            else
                            {
                                b.Append("<li><a href='/" + Cate + "&page=" + i.ToString() + Extention + "' >" +
                                         i.ToString() + "</a></li>");
                            }
                        }
                    }
                }
                else
                {
                    // ThanhDT update paging for duplicate page
                    //b.Append("<li><a href='/" + Cate + "/trang-1" + Extention + "'>1</a></li>");
                    /*b.Append("<li><a href='/" + Cate + Extention + "'>1</a></li>");
                    b.Append("<li><a href='/" + Cate + "/trang-2" + Extention + "'>2</a></li>");
                    b.Append("<li><a href='/" + Cate + "/trang-3" + Extention + "'>3</a></li>");
                    b.Append("<li>...</li>");*/

                    for (int i = (Index - 3); i <= (Index + 3); i++)
                    {
                        if (i == Index)
                        {
                            b.Append("<li class=\"active\"><a href=\"javascript:void(0)\">" + i.ToString() + "</a></li>");

                        }
                        else
                        {
                            b.Append("<li><a href='/" + Cate + "&page=" + i.ToString() + Extention + "' >" + i.ToString() + "</a></li>");
                        }
                    }
                }

                if (Index >= 1)
                {
                    b.Append("<li class=\"next\"><a href='/" + Cate + "&page=" + (Index + 1) + Extention + "' >»</a></li>");
                }
                else
                {
                    b.Append("<li class=\"next\"><a href='/" + Cate + "&page=" + Index + Extention + "' >»</a></li>");
                }

                this.ltrPaging.Text = b.ToString();
            }
            catch
            { }
        }
    }
}